package com.byyy.ccts.core.exception;

import com.byyy.ccts.core.base.ILogBase;
import com.byyy.ccts.core.message.rocketmq.OAProducer;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.utils.SpringContextHolder;
import com.byyy.ccts.modules.common.exception.BusinessAjaxException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class DefaultExceptionHandler implements ILogBase {

	/**
	 * 没有权限 异常
	 * <p/>
	 * 后续根据不同的需求定制即可
	 */
	@ExceptionHandler({ UnauthorizedException.class })
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public ModelAndView processUnauthenticatedException(
			NativeWebRequest request, UnauthorizedException e) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("exception", e);
		mv.setViewName("error/unauthorized");
		logger.error("用户权限错误", e);
		return mv;
	}

	/**
	 * Ajax请求业务异常
	 * <p/>
	 */
	@ExceptionHandler({ BusinessAjaxException.class })
	@ResponseBody
	public AjaxJson processBusinessAjaxException(
			NativeWebRequest request, BusinessAjaxException e) {
		AjaxJson json = new AjaxJson();
		json.fail("操作失败-{0}", new Object[]{ e.getMessage() });
		logger.error("用户操作错误", e);
		return json;
	}

	@ExceptionHandler(value = {BindException.class, MethodArgumentNotValidException.class})
	@ResponseBody
	public AjaxJson validationExceptionHandler(Exception e) {
		AjaxJson json = new AjaxJson();
		BindingResult result = null;
		if (e instanceof BindException) {
			result = ((BindException) e).getBindingResult();
		}
		else if (e instanceof MethodArgumentNotValidException) {
			result = ((MethodArgumentNotValidException) e).getBindingResult();
		}
		String msg = null;
		if (result != null && result.hasErrors()) {
			msg = result.getAllErrors().get(0).getDefaultMessage();
			if (msg.contains("NumberFormatException")) {
				msg = "参数类型错误！";
			}
		}
		else {
			msg = "系统繁忙，请稍后再试...";
		}

		json.fail(msg);
		return json;
	}

	/**
	 * 未捕获异常
	 * <p/>
	 */
	@ExceptionHandler({ Exception.class })
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public ModelAndView processException(
			NativeWebRequest request, Exception e) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("exception", e);
		mv.setViewName("error/500");
		OAProducer oAProducer = (OAProducer) SpringContextHolder.getBean("oAProducer");
		oAProducer.sendOAMail("exception", "未捕获异常", e.getMessage());
		logger.error("未明确捕获异常", e);
		return mv;
	}

}
